//
//  MoviePlayViewController.m
//  iBaby365v2
//
//  Created by admin on 14/11/27.
//  Copyright (c) 2014年 suplus. All rights reserved.
//

#import "MoviePlayViewController.h"
#import "AncdaPlayer.h"
#import "MBProgressHUD+MJ.h"

@interface MoviePlayViewController ()<AncdaPlayerDelegate,UIActionSheetDelegate>

@property (strong, nonatomic) AncdaPlayer *m_player;  //播放器
@end

@implementation MoviePlayViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //构造player对象
//    [self.view setRestorationIdentifier:uiview]
   
    self.view.center = CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2);
    CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI/2);
    [self.view setTransform:transform];
    
    self.view.frame = self.view.bounds;
    self.m_player = [[AncdaPlayer alloc] initWithFrame:self.view.bounds];
    
//    self.m_player.center = CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2);
//    CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI/2);
//    [self.m_player setTransform:transform];
    
    self.m_player.frame = self.view.bounds;
    
    [self.view addSubview:self.m_player];
    
    self.m_player.backgroundColor = [UIColor blackColor];
    
    [self onPlay];
}

-(BOOL)prefersStatusBarHidden
{
    return YES;
}

-(void)viewDidAppear:(BOOL)animated
{
    if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) {
        [self prefersStatusBarHidden];
        [self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)];
    }
}

-(void) mouseDown
{
    [self updateState:@"dianji"];

    UIActionSheet *sheet=[[UIActionSheet alloc] initWithTitle:@"是否决定退出" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles: nil];
    [sheet showInView:self.view];
    
}
-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    
    if (buttonIndex==0) {
        [self updateState:@"didismiss"];
        [self.delegate moviePlayerDidFinished];
        
        [self onStop];
    }
    
}
- (void)onStop
{
    [self updateState:@"onstop"];
    self.m_player.delegate = nil ;
    
    [self.m_player stop];
    
    [self.m_player clean];
}

-(void)updateState:(NSString*)s
{
    
    //    labelState.text = s;
//    [MBProgressHUD hideHUD];
//    [MBProgressHUD showError:s];
    NSLog(@"提示---》%@",s);
}

-(void)showInfo:(NSString* )s
{
    
    UIActionSheet *sheet=[[UIActionSheet alloc] initWithTitle:s delegate:nil cancelButtonTitle:@"确定" destructiveButtonTitle:nil otherButtonTitles: nil];
    [sheet showInView:self.view];
    
}
//播放事件回调
-(void) OnAncdaPlayerState:(StreamState)state lparam:(long)lparam wparam:(long)wparam
{
    
    
    
    
    if (self.m_player == nil) return;
    
    __weak id weakSelf = self;
    
    
    
    
    dispatch_async(dispatch_get_main_queue(), ^(void)
                   {
                       
                       
                       
                       
                       switch (state) {
                               
                           case StreamReConnecting:
                               
                           {
                               
                               [weakSelf updateState:@"正在重连..."];
                               break;
                               
                           }
                           case StreamLogining: {
                               
                               [weakSelf updateState:@"正在登录..."];
                               
                               break;
                               
                           }
                           case StreamConneting: {
                               [weakSelf updateState:@"正在连接..."];
                               
                               break;
                           }
                               
                           case StreamOpening:
                           {
                               
                               [weakSelf updateState:@"正在打开..." ];
                               
                               
                               break;
                               
                           }
                               
                           case StreamPlaying:
                               [weakSelf updateState:@"正在播放..." ];
                               
                               
                               break;
                           case StreamPaused:
                               break;
                           case StreamStopped:
                               
                           {
                               
                               
                           }
                               break;
                               
                               
                               
                           case StreamTalkState:
                               
                           {
                               
                               
                               switch (lparam)
                               
                               {
                                   case 1:
                                       
                                       [weakSelf updateState:@"正在通话..." ];
                                       
                                       break;
                                   case StreamChannelFailed:
                                       [weakSelf updateState:@"通道占用..." ];
                                       [self.m_player stopTalk];
                                       
                                       break;
                                   case StreamRightFailed:
                                       [weakSelf updateState:@"设备不支持..." ];
                                       [self.m_player stopTalk];
                                       
                                       
                                       break;
                                       
                                       
                                       
                               }
                               
                               
                           }
                               
                           case StreamEndReached:
                               
                               
                           {
                               
                           }
                               break;
                           case StreamEncounteredError:
                           {
                               
                               
                               [self.m_player stop];
                               switch (wparam)
                               
                               {
                                       
                                   case StreamNetworkFailed:
                                       
                                   {
                                       
                                       
                                       [weakSelf updateState:@"播放时网络连接失败."];
                                       
                                       break;
                                   }
                                   case StreamLoginFailed:
                                       
                                   {
                                       [weakSelf updateState:@"播放时用户认证失败." ];
                                       
                                       
                                       break;
                                   }
                                   case StreamChannelFailed:
                                       
                                   {
                                       
                                       [weakSelf updateState:@"播放时视频源已经离线." ];
                                       
                                       break;
                                   }
                                   case StreamOtherFailed:
                                       
                                   {
                                       [weakSelf updateState:@"播放时出现其他错误." ];
                                       
                                       break;
                                   }
                                       
                               }
                           }
                               
                               
                               
                           default:
                               
                               
                               break;
                       }
                   });
    
}
- (void)onPlay
{
    
    if (self.m_session == nil)
    {
        [self showInfo:@"请先登录."];
    }
    
    
    //注意：播放只是演示播放第一个摄像机
    
//    if (self.m_camraList.count == 0)
//    {
//        
//        [self updateState:@"当前没有摄像机."];
//    }
    
    //    AncdaCamera * camera = [self.m_camraList firstObject];
    
    if (self.camera.online == false)
    {
        
        [self showInfo:@"摄像机不在线."];
    }
   
    
    self.m_player.ancdaSession = self.m_session;
    self.m_player.EnableAudio = NO;  //关闭音频
    self.m_player.EnableAlarm = NO;  //不显示报警在图像上
    self.m_player.Bandwidth = NETWORK_AUTO; //0表示自动网络环境,1表示2G,2标识3G，3标识WIFI
    self.m_player.fillMode = SURFACE_BEST_FIT;//图像按照比例缩放
    
    self.m_player.delegate =self;
    
    [self.m_player play:self.camera.key];
}

-(void)dealloc
{
    if (self.m_player)
    {   self. m_player.delegate = nil ;
        
        
        [self.m_player stop];
    }

}
@end
